//
//  HMStatusRetweetView.swift
//  MicroBlog
//
//  Created by Apple on 16/9/23.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import YYText
//2.转发微博视图
class HMStatusRetweetView: UIView {
    //记录视图底部约束
    var retweetViewBottomConstraint: Constraint?
    var statusViewModel: HMStatusViewModel? {
        didSet {
//            contenLabel.text = statusViewModel?.retweetContent
            //转发微博富文本
            contenLabel.attributedText = statusViewModel?.retweetAttributedString
            //卸载上次的约束
            retweetViewBottomConstraint?.uninstall()
            //判断是否有配图
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0{
                //有图显示,绑定数据
                pictureView.isHidden = false
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTableViewCellMargin).constraint
                })
                //转发微博配图数据
                pictureView.picUrls = statusViewModel?.status?.retweeted_status?.pic_urls
            } else {
                pictureView.isHidden = true
                self.snp_updateConstraints(closure: { (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contenLabel).offset(HMHomeTableViewCellMargin).constraint
                })
            }
            
        }
    }
    
    //懒加载
    private lazy var contenLabel: YYLabel = {
//       let label = UILabel(textColor: UIColor.lightGray, fontSIze: HMHomeTableViewCellFontSize)
        let label = YYLabel()
        //设置最大换行宽度
        label.preferredMaxLayoutWidth = ScreenWidth - 2 * HMHomeTableViewCellMargin
        label.numberOfLines = 0
          return label
    }()
    
    //配图对象
    private lazy var pictureView: HMStatusPictureView = {
       let view = HMStatusPictureView()
        //配图和背景色相同
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contenLabel)
        addSubview(pictureView)
        
        contenLabel.snp_makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(HMHomeTableViewCellMargin)
            make.width.equalTo(ScreenWidth - 2 * HMHomeTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contenLabel.snp_bottom).offset(HMHomeTableViewCellMargin)
            make.leading.equalTo(contenLabel)
        }
        //向上的约束
        self.snp_makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(HMHomeTableViewCellMargin).constraint
        }
    }
}
